//
//  SLAddNewFriendViewController.m
//  StarLight
//
//  Created by Duke.wu on 16/6/2.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLAddNewFriendViewController.h"
#import <AddressBook/AddressBook.h>
#import "SLAddNewFriendViewModel.h"
#import "SLOperFriendTableViewCell.h"
#import "SLFriendManager.h"
#import "SLFriendAddViewController.h"
#import "STPopup.h"




@interface SLAddNewFriendViewController ()<UITableViewDelegate, UITableViewDataSource,SLOperFriendTableViewCellDelegate>

@property (nonatomic, strong) UIButton *synBtn;

@property (nonatomic, strong) SLAddNewFriendViewModel *viewModel;

@property (nonatomic, strong) UITableView *operListView;

@property (nonatomic, strong) SLFriendManager *friendMg;

@end

@implementation SLAddNewFriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  [self setUpUI];
  [self addConstrains];
  [self addObservers];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  [self.navigationController setNavigationBarHidden:NO animated:YES];
  self.view.backgroundColor = [UIColor defeutBackgroudColor];
  [self.viewModel getOperbuddylist];
  self.title = @"添加好友";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addObservers{
  @weakify(self);
  [[RACObserve(self.viewModel, operbuddyList) filter:^BOOL(NSMutableArray  *value) {
    return value != nil;
  }] subscribeNext:^(id x) {
    @strongify(self)
    [self.operListView reloadData];
    [[NSNotificationCenter defaultCenter] postNotificationName:kFriendChange object:nil];
  }];
}

- (void)synAdressPerson{
  [self.viewModel synAddresser];
}


- (void)addConstrains{
  
  [self.synBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.width.centerX.equalTo(self.view);
    make.height.equalTo(@(50*MIX_W));
    make.top.equalTo(self.mas_topLayoutGuideBottom).offset(8);
  }];
  
  [self.operListView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.width.centerX.equalTo(self.view);
    make.top.equalTo(self.synBtn.mas_bottom).offset(8);
    make.bottom.equalTo(self.mas_bottomLayoutGuideTop);
  }];
}

- (void)setUpUI{
  [self.view addSubview:self.synBtn];
  [self.view addSubview:self.operListView];
}


#pragma mark - SLOperFriendTableViewCellDelegate
- (void)didTapedoperFriendBtnWith:(SLOperbuddyModel *)model{
  
  @weakify(self)
  switch (model.state) {
    case -1:{
      // 取消屏蔽
      [self.friendMg operFriendWithHuid:model.userId opers:@"3" block:^{
        @strongify(self)
        [self restart];
      }];
    }
      break;
    case 0:{
      // 添加
      SLFriendAddViewController *addFVC = [SLFriendAddViewController new];
      addFVC.model = model;
      STPopupController *popVC = [[STPopupController alloc] initWithRootViewController:addFVC];
      popVC.navigationBarHidden = YES;
      popVC.cornerRadius = 5;
      [popVC presentInViewController:self];
    }
      break;
    case 2:{
      // 接受
      [self.friendMg operFriendWithHuid:model.userId opers:@"1" block:^{
        @strongify(self)
        [self restart];
      }];
    }
      break;
    default:
      break;
  }

  
}

- (void)restart{
  [self.viewModel getOperbuddylist];
}


- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
  if (!self.reach.isReachable) {
    return [[NSAttributedString alloc] initWithString:@"无网络连接, 请连接网络后刷新" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
  }
  return [[NSAttributedString alloc] initWithString:@"暂无联系人" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  return self.viewModel.operbuddyList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  SLOperFriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  cell.delegate = self;
  return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(SLOperFriendTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
  [cell configOperFriendCellWithModel:self.viewModel.operbuddyList[indexPath.row]];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  return 90;
}


#pragma mark - getter

-(SLFriendManager *)friendMg{
  if (!_friendMg) {
    _friendMg = [[SLFriendManager alloc] init];
  }
  return _friendMg;
}

- (UIButton *)synBtn{
  if (!_synBtn) {
    _synBtn = [UIButton buttonWithTitle:@"同步联系人"];
    [_synBtn setTitleColor:SLYellowColor forState:UIControlStateNormal];
    _synBtn.backgroundColor = [UIColor whiteColor];
    [_synBtn addTarget:self action:@selector(synAdressPerson) forControlEvents:UIControlEventTouchUpInside];
  }
  return _synBtn;
}

- (SLAddNewFriendViewModel *)viewModel{
  if (!_viewModel) {
    _viewModel = [SLAddNewFriendViewModel new];
  }
  return _viewModel;
}

- (UITableView *)operListView{
  if (!_operListView) {
    _operListView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [_operListView registerNib:[UINib nibWithNibName:@"SLOperFriendTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    _operListView.tableFooterView = [UIView new];
    _operListView.delegate = self;
    _operListView.dataSource = self;
    _operListView.emptyDataSetSource = self;
  }
  return _operListView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
